﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CustomFilters.WebPartCode;

namespace CustomFilters.UserControls
{
    public partial class DependantDropDownFilter : System.Web.UI.UserControl, IHostedUserControlFilter
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ///This is a fix needed by the Ajax Panel to be compatible with SharePoint
            ScriptManager.RegisterStartupScript(this, this.GetType(), "AjaxFix", 
                "<script type='text/javascript'>_spOriginalFormAction = document.forms[0].action; _spSuppressFormOnSubmitWrapper=true;</script>"
                , false);

            if (!Page.IsPostBack)
            {
                ///Force the state drop down to select the city upon load
                cboState_SelectedIndexChanged(null, null);
            }
        }

        protected void cboState_SelectedIndexChanged(object sender, EventArgs e)
        {
            cboCity.Items.Clear();
            switch (cboState.SelectedValue)
            {
                case "TX":
                    cboCity.Items.Add("Dallas");
                    cboCity.Items.Add("Houston");
                    cboCity.Items.Add("San Antonio");
                    cboCity.Items.Add("Austin");
                    break;
                case "TN":
                    cboCity.Items.Add("Chatenooga");
                    break;
                case "OK":
                    cboCity.Items.Add("OKC");
                    cboCity.Items.Add("Tulsa");
                    break;
                default:
                    break;
            }
        }

        #region IHostedUserControlFilter Members

        public List<string> Values
        {
            get
            {
                return new List<string>(new string[] { cboCity.SelectedValue });
            }
            set
            {
                if (value.Count > 0)
                {
                    string City = value[0];
                    switch (City)
                    {
                        case "OKC":
                        case "Tulsa":
                            cboState.Items.FindByValue("OK").Selected = true;
                            cboState_SelectedIndexChanged(null, null);
                            cboCity.Items.FindByValue(City).Selected = true;
                            break;
                        case "Chatenooga":
                            cboState.Items.FindByValue("TN").Selected = true;
                            cboState_SelectedIndexChanged(null, null);
                            cboCity.Items.FindByValue(City).Selected = true;
                            break;
                        case "Dallas":
                        case "Houston":
                        case "Austin":
                        case "San Antonio":
                            cboState.Items.FindByValue("TX").Selected = true;
                            cboState_SelectedIndexChanged(null, null);
                            cboCity.Items.FindByValue(City).Selected = true;
                            break;
                        default:
                            break;
                    }
                }
            }
        }

        #endregion
    }
}